package gd.qaq.blue.flauntitem.listener;

import gd.qaq.blue.flauntitem.FlauntItem;
import lk.vexview.event.KeyBoardPressEvent;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import static gd.qaq.blue.flauntitem.util.PluginHelper.colorFormat;
import static gd.qaq.blue.flauntitem.util.PluginHelper.error;

public class KeyListener implements Listener {
    public static Map<String, Long> playerAndCooldowns = new HashMap<>();

    @EventHandler
    public void onKeyPressed(KeyBoardPressEvent event) {
        Player player = event.getPlayer();
        int keyCode = event.getKey();
        if (keyCode == FlauntItem.config.getInt("plugin.keyCode")) {
            ItemStack itemInHand = player.getItemInHand();
            //如果玩家没拿东西
            if (itemInHand.getType().equals(Material.AIR)) {
                return;
            }
            String uuid = player.getUniqueId().toString();
            Long currentTime = System.currentTimeMillis();
            if (playerAndCooldowns.containsKey(uuid)) {
                Long lastTime = playerAndCooldowns.get(uuid);
                int second = Integer.parseInt(String.valueOf((currentTime - lastTime) / 1000));
                if (second <= FlauntItem.config.getInt("plugin.coolDown")) {
                    if (second != 0) {
                        error(player, "&c你还需要等待&e" + ((FlauntItem.config.getInt("plugin.coolDown") - second) + 1) + "&c秒才能再次炫耀物品!");
                    }
                    return;
                }
            }
            playerAndCooldowns.put(uuid, currentTime);
            String itemTypeName = "";
            itemTypeName = itemInHand.getType().name();
            String lores = "[]";
            if (itemInHand.hasItemMeta() && itemInHand.getItemMeta().hasLore())
                lores = (player.getItemInHand().getItemMeta().getLore().toString());
            String displayName = "";
            if (itemInHand.hasItemMeta() && itemInHand.getItemMeta().hasDisplayName()) {
                displayName = itemInHand.getItemMeta().getDisplayName();
            }
            String string2 = "[";
            for (final String s2 : Arrays.asList(itemInHand.getEnchantments().keySet().toString().replaceAll("Enchantment", "").replaceAll("[^A-Z_]+", " ").trim().split(" "))) {
                try {
                    string2 = String.valueOf(string2) + "{id:" + Enchantment.getByName(s2).getId() + ",lvl:" + itemInHand.getEnchantmentLevel(Enchantment.getByName(s2)) + "},";
                } catch (Exception ex2) {
                }
            }
            String string3 = String.valueOf(string2.substring(0, string2.length() - 1)) + "]";
//            String replace = lores.replace(":", ";");
            if (string3.length() < 6) {
                string3 = "[]";
            }

            for (Player onlinePlayer : Bukkit.getOnlinePlayers()) {
                Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
                        String.valueOf(new StringBuilder(String.valueOf(
                                new StringBuilder(
                                        "tellraw " + onlinePlayer.getName() + " {\"text\":\"\",\"extra\":[{\"text\":\"")
                                        .append(colorFormat("&e玩家 " + player.getDisplayName())).
                                        append("\",\"color\":\"white\"},{\"text\":\"" + colorFormat("&e 展示了他的物品") + ":\",\"color\":\"white\"},").toString())).
                                append("{\"text\":\"").
                                append(StringUtils.isNotBlank(displayName) ? displayName : itemTypeName).
                                append("\",\"color\":\"aqua\",\"hoverEvent\":{\"action\":\"show_item\",\"value\":\"{id:322,tag:{display:{Name:").
                                append(StringUtils.isNotBlank(displayName) ? displayName : colorFormat("&f" + itemTypeName)).append(",Lore:").append(lores).append("},ench:").
                                append(string3).append("}}\"}}").toString()) + "]}");
            }
        }
    }
}
